package com.pro.addresslist.dal.bll;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.pro.addresslist.dal.model.IPMAC;
import com.pro.addresslist.tools.EncryptInterface;
import com.pro.addresslist.tools.EncryptRequest;
import com.pro.addresslist.tools.JSONHelper;
import com.pro.addresslist.tools.URLUtil;

public class IPMACBLL {
	
	/**
	 * 查询绑定手机
	 */
	public static void SelectPhoneTest(Context mContext){
		final List<IPMAC> list=new ArrayList<IPMAC>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectPhones");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forPhone, "SelectPhones", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				Log.i("libs", "1");
				if(result.has("result"))
				{
					Log.i("libs", "1");
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 IPMAC[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, IPMAC.class);
					 for (IPMAC IpMac : uu) {
						 list.add(IpMac);
					}
					 Log.i("libs", "1");
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectPhones");  
		             mContext.sendBroadcast(intent);
		             Log.i("libs", "1");
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}

	/**
	 * 查询绑定手机
	 */
	public static void SelectPhone(Context mContext){
		final List<IPMAC> list=new ArrayList<IPMAC>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectPhone");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forPhone, "SelectPhone", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 IPMAC[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, IPMAC.class);
					 for (IPMAC IpMac : uu) {
						 list.add(IpMac);
				        	Log.i("bt","SelectPhone+value==>"+ IpMac.get_MACorTELEn() );			        	
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectPhone");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "连接服务器超时，请稍后再试！", Toast.LENGTH_SHORT).show();
				Log.i("bt", "SelectPhone err-->"+error.getMessage());
			}
		});
	}
	/**
	 * 修改绑定手机
	 */
	public  static void  UpdataPhone(Context mContext,IPMAC IpMac){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "UpdataPhone");
		mMap.put("IPMac", JSONHelper.toJSON(IpMac));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forPhone, "UpdataPhone", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String results = result.optString("result");
		        	Log.i("bt","UpdataPhone+value==>"+ results );

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter", results);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_UpdataPhone");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 添加绑定手机
	 */
	public  static void  AddPhone(Context mContext,IPMAC IpMac){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AddPhone");
		mMap.put("IPMac", JSONHelper.toJSON(IpMac));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forPhone, "AddPhone", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String results = result.optString("result");
				 Log.i("bt","AddPhone+value==>"+ results );
				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter", results);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_AddPhone");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
				//Toast.makeText(mContext, "服务器错误！"+ error.getMessage(), Toast.LENGTH_SHORT).show();
				 Log.i("bt","AddPhone+value==>"+ error.getMessage() );

			}
		});
	}
	/**
	 * 删除绑定手机
	 */
	public  static void  DelPhone(Context mContext,String IpMac){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "DelPhone");
		mMap.put("IPMacID", IpMac);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forPhone, "DelPhone", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String results = result.optString("result");
				 Log.i("bt","DelPhone+value==>"+ results );
				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter", results);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_DelPhone");  
	             mContext.sendBroadcast(intent);
				} 
			}
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
				 Log.i("bt","AddPhone+value==>"+ error.getMessage() );

			}
		});
	}
	/**
	 * 验证绑定手机
	 */
//	public Boolean YanPhone(Context mContext){
//		TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);  
//		  String deviceid = tm.getDeviceId();//获取智能设备唯一编号  
//		  String te1  = tm.getLine1Number();//获取本机号码  
//		  String imei = tm.getSimSerialNumber();//获得SIM卡的序号  
//		  String imsi = tm.getSubscriberId();//得到用户Id  
//		  List<IPMAC> list=SelectPhone(mContext);
//		  Boolean result=false;
//		  if(list!=null){
//			  for(IPMAC ipMac:list){
//				  result= ipMac.equals(imei);
//				  if(result){break;}
//			  }
//		  }
//		return result;
//	}
	
	public  static void  Test(Context mContext){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectPhone");
		EncryptRequest.RequestJsonPost(mContext, "https://www.baidu.com", "SelectPhone", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				//Toast.makeText(mContext, "test"+result, Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(); 
                Bundle bundle = new Bundle();
                bundle.putSerializable("parameter", "s");
                intent.putExtras(bundle);
                intent.setAction("lbs_parameter_LastInfo");  
                mContext.sendBroadcast(intent);
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
		
	}


	/**
	 * 查询版本号
	 */
	public static void SearchVersion(Context mContext){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SearchVersion");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forAppversion, "SearchVersion", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				
				if(result.has("result"))
				{
					 String results = result.optString("result");			 
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",results);
		             intent.putExtras(bundle);
		             intent.setAction("SearchVersion");  
		             mContext.sendBroadcast(intent);
				 
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				//Toast.makeText(mContext, "连接服务器超时，请稍后再试！", Toast.LENGTH_SHORT).show();
				//String results = result.optString("result");			 
				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter","err");
	             intent.putExtras(bundle);
	             intent.setAction("SearchVersion");  
	             mContext.sendBroadcast(intent);
				Log.i("bt","SearchVersion"+error.getMessage());
				//Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}

}
